<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>从尾到头打印链表</title>
</head>
<body>
  <div>输入一个链表的头节点，从尾到头反过来返回每个节点的值（用数组返回）</div>
  <div>
    <span>解题思路</span>
    链表中取出数存到数组A中，然后把数组A的数pop到数组B中
  </div>
  <script>
    /**
     * Definition for singly-linked list.
     * function ListNode(val) {
     *     this.val = val;
     *     this.next = null;
     * }
     */
    /**
     * @param {ListNode} head
     * @return {number[]}
     */
    var reversePrint = function(head) {
      let stack1 = [], stack2 = [];
      while(head) {
        stack1.push(head.val);
        head = head.next;
      }
      let aLength = stack1.length;
      for (let i = 0; i < aLength; i++) {
        stack2[i] = stack1.pop();
      }
      return stack2;
    };
  </script>
</body>
</html>